<?php

class Adept_Db_MySql_TableInfo extends Adept_Db_TableInfo 
{
    
    const NOT_NULL_FLAG = 'not_null';
    const PRIMARY_KEY_FLAG = 'primary_key';
        
    /**
     * @throws Adept_Db_Exception 
     */
    protected function retriveMetadata()
    {        
        $this->columns = array();
        
        $resource = mysql_query('SELECT * FROM `' . $this->tableName . '` LIMIT 1', $this->connection->getConnectionId());
        if (!$resource) {
            throw new Adept_Db_Exception('Cannot retrive table metadata', 0, array('table' => $this->tableName));             
        } 
        
        $this->columnsCount = mysql_num_fields($resource);
        
        for ($i = 0; $i < $this->columnsCount; $i++) {
            $column = new Adept_Db_MySql_ColumnInfo();
            $column->setName(mysql_field_name($resource, $i));
            $column->setSize(mysql_field_len($resource, $i));
            $column->setType(mysql_field_type($resource, $i));
                
            $flags = explode(" ", mysql_field_flags($resource, $i));        
                
            if (in_array(self::PRIMARY_KEY_FLAG, $flags)) {
                $column->setPrimaryKey(in_array(self::PRIMARY_KEY_FLAG, $flags));
                $this->primaryKey = $column->getName();
            }
            $column->setNotNull(in_array(self::NOT_NULL_FLAG, $flags));
            
            $this->columns[$column->getName()] = $column;
         }
    }
    
}